using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace SubSonicResearch
{
	/// <summary>
	/// Strongly-typed collection for the Address class.
	/// </summary>
    [Serializable]
	public partial class AddressCollection : ActiveList<Address, AddressCollection>
	{	   
		public AddressCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>AddressCollection</returns>
		public AddressCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Address o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Addresses table.
	/// </summary>
	[Serializable]
	public partial class Address : ActiveRecord<Address>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Address()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Address(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Address(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Address(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Addresses", TableType.Table, DataService.GetInstance("ssDSN"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarAddressId = new TableSchema.TableColumn(schema);
				colvarAddressId.ColumnName = "AddressId";
				colvarAddressId.DataType = DbType.Int32;
				colvarAddressId.MaxLength = 0;
				colvarAddressId.AutoIncrement = true;
				colvarAddressId.IsNullable = false;
				colvarAddressId.IsPrimaryKey = true;
				colvarAddressId.IsForeignKey = false;
				colvarAddressId.IsReadOnly = false;
				colvarAddressId.DefaultSetting = @"";
				colvarAddressId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAddressId);
				
				TableSchema.TableColumn colvarPersonId = new TableSchema.TableColumn(schema);
				colvarPersonId.ColumnName = "PersonId";
				colvarPersonId.DataType = DbType.Int32;
				colvarPersonId.MaxLength = 0;
				colvarPersonId.AutoIncrement = false;
				colvarPersonId.IsNullable = false;
				colvarPersonId.IsPrimaryKey = false;
				colvarPersonId.IsForeignKey = true;
				colvarPersonId.IsReadOnly = false;
				colvarPersonId.DefaultSetting = @"";
				
					colvarPersonId.ForeignKeyTableName = "People";
				schema.Columns.Add(colvarPersonId);
				
				TableSchema.TableColumn colvarIsDefault = new TableSchema.TableColumn(schema);
				colvarIsDefault.ColumnName = "IsDefault";
				colvarIsDefault.DataType = DbType.Boolean;
				colvarIsDefault.MaxLength = 0;
				colvarIsDefault.AutoIncrement = false;
				colvarIsDefault.IsNullable = false;
				colvarIsDefault.IsPrimaryKey = false;
				colvarIsDefault.IsForeignKey = false;
				colvarIsDefault.IsReadOnly = false;
				colvarIsDefault.DefaultSetting = @"";
				colvarIsDefault.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsDefault);
				
				TableSchema.TableColumn colvarLine1 = new TableSchema.TableColumn(schema);
				colvarLine1.ColumnName = "Line1";
				colvarLine1.DataType = DbType.AnsiString;
				colvarLine1.MaxLength = 128;
				colvarLine1.AutoIncrement = false;
				colvarLine1.IsNullable = false;
				colvarLine1.IsPrimaryKey = false;
				colvarLine1.IsForeignKey = false;
				colvarLine1.IsReadOnly = false;
				colvarLine1.DefaultSetting = @"";
				colvarLine1.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLine1);
				
				TableSchema.TableColumn colvarLine2 = new TableSchema.TableColumn(schema);
				colvarLine2.ColumnName = "Line2";
				colvarLine2.DataType = DbType.AnsiString;
				colvarLine2.MaxLength = 128;
				colvarLine2.AutoIncrement = false;
				colvarLine2.IsNullable = true;
				colvarLine2.IsPrimaryKey = false;
				colvarLine2.IsForeignKey = false;
				colvarLine2.IsReadOnly = false;
				colvarLine2.DefaultSetting = @"";
				colvarLine2.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLine2);
				
				TableSchema.TableColumn colvarCity = new TableSchema.TableColumn(schema);
				colvarCity.ColumnName = "City";
				colvarCity.DataType = DbType.AnsiString;
				colvarCity.MaxLength = 128;
				colvarCity.AutoIncrement = false;
				colvarCity.IsNullable = false;
				colvarCity.IsPrimaryKey = false;
				colvarCity.IsForeignKey = false;
				colvarCity.IsReadOnly = false;
				colvarCity.DefaultSetting = @"";
				colvarCity.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCity);
				
				TableSchema.TableColumn colvarState = new TableSchema.TableColumn(schema);
				colvarState.ColumnName = "State";
				colvarState.DataType = DbType.AnsiString;
				colvarState.MaxLength = 2;
				colvarState.AutoIncrement = false;
				colvarState.IsNullable = false;
				colvarState.IsPrimaryKey = false;
				colvarState.IsForeignKey = false;
				colvarState.IsReadOnly = false;
				colvarState.DefaultSetting = @"";
				colvarState.ForeignKeyTableName = "";
				schema.Columns.Add(colvarState);
				
				TableSchema.TableColumn colvarZipCode = new TableSchema.TableColumn(schema);
				colvarZipCode.ColumnName = "ZipCode";
				colvarZipCode.DataType = DbType.AnsiStringFixedLength;
				colvarZipCode.MaxLength = 5;
				colvarZipCode.AutoIncrement = false;
				colvarZipCode.IsNullable = false;
				colvarZipCode.IsPrimaryKey = false;
				colvarZipCode.IsForeignKey = false;
				colvarZipCode.IsReadOnly = false;
				colvarZipCode.DefaultSetting = @"";
				colvarZipCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarZipCode);
				
				TableSchema.TableColumn colvarZipCodePlusFour = new TableSchema.TableColumn(schema);
				colvarZipCodePlusFour.ColumnName = "ZipCodePlusFour";
				colvarZipCodePlusFour.DataType = DbType.AnsiStringFixedLength;
				colvarZipCodePlusFour.MaxLength = 4;
				colvarZipCodePlusFour.AutoIncrement = false;
				colvarZipCodePlusFour.IsNullable = false;
				colvarZipCodePlusFour.IsPrimaryKey = false;
				colvarZipCodePlusFour.IsForeignKey = false;
				colvarZipCodePlusFour.IsReadOnly = false;
				colvarZipCodePlusFour.DefaultSetting = @"";
				colvarZipCodePlusFour.ForeignKeyTableName = "";
				schema.Columns.Add(colvarZipCodePlusFour);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ssDSN"].AddSchema("Addresses",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("AddressId")]
		[Bindable(true)]
		public int AddressId 
		{
			get { return GetColumnValue<int>(Columns.AddressId); }
			set { SetColumnValue(Columns.AddressId, value); }
		}
		  
		[XmlAttribute("PersonId")]
		[Bindable(true)]
		public int PersonId 
		{
			get { return GetColumnValue<int>(Columns.PersonId); }
			set { SetColumnValue(Columns.PersonId, value); }
		}
		  
		[XmlAttribute("IsDefault")]
		[Bindable(true)]
		public bool IsDefault 
		{
			get { return GetColumnValue<bool>(Columns.IsDefault); }
			set { SetColumnValue(Columns.IsDefault, value); }
		}
		  
		[XmlAttribute("Line1")]
		[Bindable(true)]
		public string Line1 
		{
			get { return GetColumnValue<string>(Columns.Line1); }
			set { SetColumnValue(Columns.Line1, value); }
		}
		  
		[XmlAttribute("Line2")]
		[Bindable(true)]
		public string Line2 
		{
			get { return GetColumnValue<string>(Columns.Line2); }
			set { SetColumnValue(Columns.Line2, value); }
		}
		  
		[XmlAttribute("City")]
		[Bindable(true)]
		public string City 
		{
			get { return GetColumnValue<string>(Columns.City); }
			set { SetColumnValue(Columns.City, value); }
		}
		  
		[XmlAttribute("State")]
		[Bindable(true)]
		public string State 
		{
			get { return GetColumnValue<string>(Columns.State); }
			set { SetColumnValue(Columns.State, value); }
		}
		  
		[XmlAttribute("ZipCode")]
		[Bindable(true)]
		public string ZipCode 
		{
			get { return GetColumnValue<string>(Columns.ZipCode); }
			set { SetColumnValue(Columns.ZipCode, value); }
		}
		  
		[XmlAttribute("ZipCodePlusFour")]
		[Bindable(true)]
		public string ZipCodePlusFour 
		{
			get { return GetColumnValue<string>(Columns.ZipCodePlusFour); }
			set { SetColumnValue(Columns.ZipCodePlusFour, value); }
		}
		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Person ActiveRecord object related to this Address
		/// 
		/// </summary>
		public SubSonicResearch.Person Person
		{
			get { return SubSonicResearch.Person.FetchByID(this.PersonId); }
			set { SetColumnValue("PersonId", value.PersonId); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varPersonId,bool varIsDefault,string varLine1,string varLine2,string varCity,string varState,string varZipCode,string varZipCodePlusFour)
		{
			Address item = new Address();
			
			item.PersonId = varPersonId;
			
			item.IsDefault = varIsDefault;
			
			item.Line1 = varLine1;
			
			item.Line2 = varLine2;
			
			item.City = varCity;
			
			item.State = varState;
			
			item.ZipCode = varZipCode;
			
			item.ZipCodePlusFour = varZipCodePlusFour;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varAddressId,int varPersonId,bool varIsDefault,string varLine1,string varLine2,string varCity,string varState,string varZipCode,string varZipCodePlusFour)
		{
			Address item = new Address();
			
				item.AddressId = varAddressId;
			
				item.PersonId = varPersonId;
			
				item.IsDefault = varIsDefault;
			
				item.Line1 = varLine1;
			
				item.Line2 = varLine2;
			
				item.City = varCity;
			
				item.State = varState;
			
				item.ZipCode = varZipCode;
			
				item.ZipCodePlusFour = varZipCodePlusFour;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn AddressIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn PersonIdColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn IsDefaultColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn Line1Column
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn Line2Column
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn CityColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn StateColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn ZipCodeColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn ZipCodePlusFourColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string AddressId = @"AddressId";
			 public static string PersonId = @"PersonId";
			 public static string IsDefault = @"IsDefault";
			 public static string Line1 = @"Line1";
			 public static string Line2 = @"Line2";
			 public static string City = @"City";
			 public static string State = @"State";
			 public static string ZipCode = @"ZipCode";
			 public static string ZipCodePlusFour = @"ZipCodePlusFour";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
